//
//  XQBirthdayTimeView.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/24.
//

#import "XQBirthdayTimeView.h"
#import "XQCustomOptionCell.h"

@interface XQBirthdayTimeView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic, assign) NSInteger selectRow;
@end

@implementation XQBirthdayTimeView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self makeUI];
        [self makeConstraints];
    }
    return self;
}

- (void)makeUI {
    [self addSubview:self.tableView];
}

- (void)makeConstraints {
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@0);
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.bottom.equalTo(@0);
    }];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count + 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XQCustomOptionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XQCustomOptionCell" forIndexPath:indexPath];
    if (indexPath.row == 0 || indexPath.row == 1 || indexPath.row == self.datas.count + 3 || indexPath.row == self.datas.count + 2) {
        [cell setContent:@""];
    }else {
        [cell setContent:[NSString stringWithFormat:@"%@",self.datas[indexPath.row - 2]]];
    }
    [cell setIsSelected:self.selectRow == indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0 || indexPath.row == 0 || indexPath.row == self.datas.count + 3 || indexPath.row == self.datas.count + 2) return;
    self.selectRow = indexPath.row;
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    [self.tableView reloadData];
    
    if (self.selectDateHandle) {
        self.selectDateHandle(self.datas[self.selectRow - 2]);
    }
}

#pragma mark - setter

- (void)setDatas:(NSArray *)datas {
    if (_datas.count > 0 && datas.count < _datas.count) {
        if (self.selectRow == _datas.count + 1) {
            self.selectRow = datas.count + 1;
        }
    }
    _datas = datas;
    [self.tableView reloadData];
}

- (void)setSelectData:(NSString *)selectData {
    _selectData= selectData;
    int index = 2;
    for (int i = 0; i < self.datas.count; i++) {
        if ([self.datas[i] isEqualToString:selectData]) {
            index = i;
            break;
        }
    }
    
    self.selectRow = index + 2;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.selectRow inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    });
}

#pragma mark - scrollViewDelegate

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    self.selectRow = -1;
    [self.tableView reloadData];
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    CGFloat rowHeight = 40; // 每行的高度
    CGFloat targetY = targetContentOffset->y;

    // 计算目标行
    NSInteger targetRow = (NSInteger)(targetY / rowHeight + (velocity.y > 0 ? 0.5 : -0.5));
    targetRow = MAX(0, MIN(targetRow, self.datas.count - 1)); // 确保在有效范围内

    self.selectRow = targetRow + 2;
    
    targetContentOffset->y = targetRow * rowHeight;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self.tableView reloadData];
    
    if (self.selectDateHandle) {
        self.selectDateHandle(self.datas[self.selectRow - 2]);
    }
}

#pragma mark - lazy

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.pagingEnabled = NO;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        [_tableView registerClass:[XQCustomOptionCell class] forCellReuseIdentifier:@"XQCustomOptionCell"];
    }
    return _tableView;
}
@end
